package cate.game.activity.specall;

import cate.common.table.activity.specall.row.SpeCallWarOrderBaseRow;
import cate.common.util.TimeTool;
import cate.common.util.XT;
import cate.game.GameBody;
import cate.game.activity.base.core.ActivityGlobalData;
import cate.game.activity.config.ActivityBaseCfg;
import cate.game.db.mongo.kv.KvMongo;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.List;

public class SpeCallSPO extends ActivityGlobalData {
    @NoteField("开启着的活动")
    public List<ActivityBaseCfg> openActivities;

    @NoteField("下次刷新时间")
    public long nextRefreshTime;

    @NoteField("虚假增长总人数")
    public volatile int fakeNum;

    @NoteField("虚假增长总人数预设")
    public int fakeNumDefault;

    @NoteField("配置ID")
    public int confId;

    @NoteField("次数")
    public volatile int trueNum;

    public SpeCallSPO() {
        super();
    }

    @Override
    public void initialize() {
        if (openActivities == null) {
            openActivities = new ArrayList<>();
        }
    }

    public void onOpen(GameBody game) {
        this.nextRefreshTime = base.startTime;
        this.confId = base.configTid;
        SpeCallWarOrderBaseRow row = game.table.speCall.warOrderBase.getRow(base.configTid);
        if (row != null) {
            fakeNumDefault = XT.randomRange(row.totalLowerBound, row.totalUpperBound);
        }
    }

    public void onClose() {
        clear();
    }

    private boolean checkAddAll(GameBody game) {
        if (confId == 0 || fakeNum >= fakeNumDefault) {
            return false;
        }
        SpeCallWarOrderBaseRow row = game.table.speCall.warOrderBase.getRow(confId);
        if (row == null) {
            return false;
        }
        boolean change = false;
        if (nextRefreshTime <= System.currentTimeMillis()) {
            synchronized (this) {
                if (fakeNum < fakeNumDefault && nextRefreshTime <= System.currentTimeMillis()) {
                    nextRefreshTime += TimeTool.TimeCons.MINUTE * XT.randomRange(row.dTimeLowerBound, row.dTimeUpperBound);
                    fakeNum += XT.randomRange(row.dNumLowerBound, row.dNumUpperBound);
                    fakeNum = Math.min(fakeNumDefault, fakeNum);
                    change = true;
                }
            }
        }
        return change;
    }

    public void clear() {
        confId = 0;
        fakeNum = 0;
        fakeNumDefault = 0;
        nextRefreshTime = 0;
        trueNum = 0;
        change = false;
    }

    public int getTotalNum() {
        return trueNum + fakeNum;
    }

    public void triggerFake(GameBody gameBody){
        boolean tmpChange = checkAddAll(gameBody);
        if (tmpChange) {
            change = true;
        }
    }

    public void triggerExe(GameBody gameBody){
        if (change) {
            change = false;
            noticeAll(gameBody, getTotalNum());
        }
    }
    private volatile boolean change;
    public int addTrueNum(int num) {
        if (confId == 0) {
            return 0;
        }
        synchronized (this) {
            this.trueNum += num;
            change = true;
        }
        return getTotalNum();
    }

    private void noticeAll(GameBody gameBody, int num){
        gameBody.role.activeForOnline(a -> a.getActivity().getEnableList(SpeCallActivity.class).forEach(s->s.warOrder.noticeNum(num)));
    }
}
